package br.com.cotec.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import br.com.cotec.dao.InboxDao;
import br.com.cotec.model.Inbox;

public class InboxDaoImpl extends GenericDaoImpl<Inbox, Serializable> implements InboxDao{

	/** 
	 * {@inheritDoc}
	 * 
	 * @see br.com.cotec.dao.InboxDao#getInboxGeral()
	 */
	@Override
	@Transactional
	public List<Inbox> getInboxGeral() {
		Session hibernateSession = (Session) em.getDelegate();
		Criteria criteria = hibernateSession.createCriteria(Inbox.class, "inb");
		criteria.add(Restrictions.isNull("inb.dataConclusao"));
		criteria.add(Restrictions.isNull("inb.pessoaResponsavel"));
		criteria.addOrder(Order.desc("inb.dataCriacao"));
		return criteria.list();
	}

	/** 
	 * {@inheritDoc}
	 * @see br.com.cotec.dao.InboxDao#getInboxGeralPorTecnico()
	 */
	@Override
	@Transactional
	public List<Inbox> getInboxGeralPorTecnico() {
		Session hibernateSession = (Session) em.getDelegate();
		Criteria criteria = hibernateSession.createCriteria(Inbox.class, "inb");
		criteria.add(Restrictions.isNull("inb.dataConclusao"));
		criteria.add(Restrictions.isNotNull("inb.pessoaResponsavel"));
		
		return criteria.list();
	}

	/**
	 * {@inheritDoc}
	 * @see br.com.cotec.dao.InboxDao#getInboxSolicitante()
	 */
	@Override
	@Transactional
	public List<Inbox> getMeuInbox(Long idPessoaSolicitante, Long idPessoaResponsavel) {
		Session hibernateSession = (Session) em.getDelegate();
		Criteria criteria = hibernateSession.createCriteria(Inbox.class, "inb");
		criteria.add(Restrictions.disjunction().add(
				Restrictions
						.eq("inb.pessoaSolicitante.id", idPessoaSolicitante))
				.add(
						Restrictions.eq("inb.pessoaResponsavel.id",
								idPessoaResponsavel)));
//		if (pessoaSolicitante != null) {
//			criteria.createCriteria("inb.pessoaSolicitante").add(Example.create(pessoaSolicitante));
//		}
//		
//		if (pessoaResponsavel != null) {
//			criteria.createCriteria("inb.pessoaResponsavel").add(Example.create(pessoaResponsavel));
//		}
		return criteria.list();
	}
}